from django.db.models import Q
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from course import models, sercourse
from rest_framework.pagination import PageNumberPagination
from rest_framework.generics import ListAPIView


# Create your views here.
class DirectionView(APIView):
    def get(self, request):
        direction = models.Direction.objects.all()
        ser = sercourse.DirectionSerializer(direction, many=True)
        return Response({'msg': 'ok', 'data': ser.data})


# class CateView(APIView):
#     def get(self, request):
#         cate = models.Cate.objects.all()
#         ser = sercourse.CateSerializer(cate, many=True)
#         return Response({'msg': 'ok', 'data': ser.data})
class Catef(APIView):
    def get(self, request):
        did = request.query_params.get('did')
        cate = models.Cate.objects.filter(direction=did)
        if cate:
            ser = sercourse.CateSerializer(cate, many=True)
            return Response({'msg': 'ok', 'data': ser.data}, status=200)
        elif did == str(0):
            cate = models.Cate.objects.all()
            ser = sercourse.CateSerializer(cate, many=True)
            return Response({'msg': 'ok', 'data': ser.data}, status=200)
        else:
            return Response({'msg': 'ok', 'data': '没有该方向'})


class CoursePage(PageNumberPagination):
    page_size = 3
    page_size_query_param = 'size'
    page_query_param = 'page'
    max_page_size = 100


# class Courselist(APIView):
#     pagination_class = CoursePage
#     def get(self, request):
#         cid=request.query_params.get('cid')
#         course = models.Course.objects.filter(cate=cid)
#         if course:
#             ser = sercourse.CourseSerializer(course, many=True)
#             return Response({'msg': 'ok', 'data': ser.data},status=200)
#         elif cid == str(0):
#             course1 = models.Course.objects.all()
#             ser = sercourse.CourseSerializer(course1, many=True)
#             paginator = self.pagination_class()
#             page = paginator.paginate_queryset(course1, request)
#             return Response({'msg': 'ok', 'data': ser.data,'params':page},status=200)
#         else:
#             return Response({'msg': '没有该分类','data': []}, status=200)

class Courselist(ListAPIView):
    queryset = models.Course.objects.all()
    serializer_class = sercourse.CourseSerializer
    pagination_class = CoursePage

    def list(self, request, *args, **kwargs):
        cid = request.query_params.get('cid')
        course = models.Course.objects.filter(cate=cid)
        lever = request.query_params.get('lever')
        if course:
            page = self.paginate_queryset(course)
            if page is not None:
                serializer = self.get_serializer(page, many=True)
                return self.get_paginated_response(serializer.data)
        elif cid == str(0):
            queryset = models.Course.objects.all()
            page = self.paginate_queryset(queryset)
            if page is not None:
                serializer = self.get_serializer(page, many=True)
                return self.get_paginated_response(serializer.data)
        elif lever:
            queryset = models.Course.objects.filter(level=lever)
            page = self.paginate_queryset(queryset)
            if page is not None:
                serializer = self.get_serializer(page, many=True)
                return self.get_paginated_response(serializer.data)
        else:
            return Response({'msg': '没有该分类', 'data': []}, status=200)


class Levellist(APIView):
    def get(self, request):
        level = models.Level.objects.all()
        ser = sercourse.LevelSerializer(level, many=True)
        return Response({'msg': 'ok', 'data': ser.data}, status=200)


# class CourseleverList(ListAPIView):
#     queryset = models.Course.objects.all()
#     serializer_class = sercourse.CourseSerializer
#     pagination_class = CoursePage
#
#     def list(self, request, *args, **kwargs):
#         cid = request.query_params.get('cid')
#         course = models.Course.objects.filter(cate=cid)
#         level = request.query_params.get('level')
#         if level: #level存在
#             if course:#course存在
#                 lc=course.filter(level__id=level)
#                 page = self.paginate_queryset(lc)
#                 if page is not None:#course分页存在
#                     serializer = self.get_serializer(page, many=True)
#                     return self.get_paginated_response(serializer.data)
#                 elif cid == str(0):#course分页不存在 cid为0
#                     queryset = models.Course.objects.all()
#                     page = self.paginate_queryset(queryset)
#                     if page is not None:
#                         serializer = self.get_serializer(page, many=True)
#                         return self.get_paginated_response(serializer.data)
#             else:#course不存在 等级存在
#                 queryset = models.Course.objects.filter(level=level)
#                 page = self.paginate_queryset(queryset)
#                 if page is not None:
#                     serializer = self.get_serializer(page, many=True)
#                     return self.get_paginated_response(serializer.data)
#                 elif cid == '0':
#                     queryset = models.Course.objects.all()
#                     page = self.paginate_queryset(queryset)
#                     if page is not None:
#                         serializer = self.get_serializer(page, many=True)
#                         return self.get_paginated_response(serializer.data)
#         elif level == str(0):#level不存在
#             if course:  #不存在等级 存在分类
#                 page = self.paginate_queryset(course)
#                 if page is not None:
#                     serializer = self.get_serializer(page, many=True)
#                     return self.get_paginated_response(serializer.data)
#             elif cid == str(0): #不存在等级 不存在分类
#                 queryset = models.Course.objects.all()
#                 page = self.paginate_queryset(queryset)
#                 if page is not None:
#                     serializer = self.get_serializer(page, many=True)
#                     return self.get_paginated_response(serializer.data)
# class lever(APIView):
#     def get(self, request):
#         cid = request.query_params.get('cid')
#         level = request.query_params.get('level')
#         if level:
#             if cid:
#                 course = models.Course.objects.filter(cate=cid)
#                 if course:
#                     lc = course.filter(level__id=level)
#                     ser = sercourse.CourseSerializer(lc, many=True)
#                     return Response({'msg': 'ok', 'data': ser.data}, status=200)
#                 else:
#                     queryset = models.Course.objects.filter(level=level)
#                     ser = sercourse.CourseSerializer(queryset, many=True)
#                     return Response({'msg': 'ok', 'data': ser.data}, status=200)
#             else:
#                 queryset = models.Course.objects.filter(level=level)
#                 ser = sercourse.CourseSerializer(queryset, many=True)
#                 return Response({'msg': 'ok', 'data': ser.data}, status=200)
#         else:
#             if cid:
#                 queryset = models.Course.objects.filter(cate=cid)
#                 ser = sercourse.CourseSerializer(queryset, many=True)
#                 return Response({'msg': 'ok', 'data': ser.data}, status=200)
#             else:
#                 queryset = models.Course.objects.all()
#                 ser = sercourse.CourseSerializer(queryset, many=True)
#                 return Response({'msg': 'ok', 'data': ser.data}, status=200)
class CourseleverView(ListAPIView):
    queryset = models.Course.objects.all()
    serializer_class = sercourse.CourseSerializer
    pagination_class = CoursePage
    def get_queryset(self):
        query = Q(isshow=True)
        # 获取到前端传过来的参数
        cateid = self.request.query_params.get("cateid")
        directionid = self.request.query_params.get("directionid")
        level = self.request.query_params.get("level")
        # 前端传递的参数
        orderdata = self.request.query_params.get("ordering")

        if level:
            query = query & Q(level=level)
        if cateid:
            # 如果cateid不为空 就不用管 directionid 只是检索 cateid
            query = query & Q(category=cateid)
        else:
            # 如果cateid为空 directionid不为空
            if directionid:
                # 根据directionid拿到所有的二级分类  [son1,son2,son3]
                son = models.Cate.objects.filter(parent=directionid)
                sondata = []
                for i in son:
                    sondata.append(i.id)
                query = query & Q(category__in=sondata)
        if orderdata:
            queryset = models.Cate.objects.filter(query).order_by("order", orderdata)
        else:
            queryset = models.Cate.objects.filter(query).order_by("order")
        return queryset


class CourseDetailView(APIView):
    def get(self, request):
        id = request.query_params.get('id')
        queryset = models.Course.objects.filter(id=id).first()
        ser = sercourse.CourseSerializer(queryset)
        return Response({'msg': 'ok', 'data': ser.data}, status=200)
